package com.xiaochen.manager.controller;

import com.xiaochen.manager.service.CategoryService;
import com.xiaochen.model.entity.product.Category;
import com.xiaochen.model.vo.common.Result;
import com.xiaochen.model.vo.common.ResultCodeEnum;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 商品分类
 */
@Slf4j
@RestController
@RequestMapping("/admin/product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 1.查询所有分类列表,每次只查询一层,用户点击了再查询下一层
     * select * from category where parent_id = id
     * @return
     */
    @GetMapping("/findCategoryList/{id}")
    public Result findCategoryList(@PathVariable Long id){
        List<Category> categoryList = categoryService.findCategoryList(id);
        return Result.build(categoryList, ResultCodeEnum.SUCCESS);
    }

    /**
     * 2.批量导入
     * 用EasyExcel实现
     */
    @GetMapping("/exportExcelData")
    public void exportExcelData(HttpServletResponse response){
        categoryService.exportExcelData(response);
    }

    /**
     * 批量导入分类数据
     * @param file
     * @return
     */
    @PostMapping("/importExcelData")
    public Result importExcelData(MultipartFile file){
        categoryService.importExcelData(file);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
